_fnc_repeated_purchase = {
	for "_x" from 1 to 3 do {
		_money = player getVariable "money";
		if ((_money - _price) >= 0) then {
			player addMagazine (_this select 0);
			[-_price] call fnc_modify_money;
		};		
	};
};

/* PURCHASE DAT GUN */

/* classname, price */

_specialItems = ["SmokeShell", "ACE_JerryCan_Dummy_15", "ACE_Flashbang", "MAG_PRIMARY", "MAG_SECONDARY"];

_args = [];
if (count _args > 2) then {
	_args = _this select 3;
} else {
	_args = _this;
};

/* Check if the player can afford this before doing anything else */
_money = player getVariable "money";
_price = _args select 1;
_cancel = false;

/* 
	For some reason I keep getting totally retarded "Error Missing ;" at any attempt of using scopes/break or exitWith so for now _cancel is a working solution
*/

if ((_money - _price) >= 0) then {
	if ((_args select 0) in _specialItems) then {
		_weapons = [] call fnc_player_getWeapons;
		switch (_args select 0) do {
			case "SmokeShell": {
				player addMagazine "SmokeShell";
			};
			case "ACE_JerryCan_Dummy_15": {
				player addWeapon "ACE_JerryCan_Dummy_15";
			};
			case "ACE_Flashbang": {
				/* TODO: Make sure a player can only buy a maximum of two at any given time */
				player addMagazine "ACE_Flashbang";
			};
			case "MAG_PRIMARY": {
				if !((_weapons select 0) == "") then {
					_magazine = getArray (configFile >> "CfgWeapons" >> (_weapons select 0) >> "magazines") select 0;

					/* Check if the weapon has an assigned preferred magazine */
					_pref = [_weapons select 0, 0, prefMags] call fnc_search_array;
					if (_pref >= 0) then {
						_magazine = (prefMags select _pref) select 1;
					};

					/* Per request, We should actually attempt to buy three magazines for each button press */
					_cancel = true;
					[_magazine] call _fnc_repeated_purchase;
				} else {
					_cancel = true;
				};
			};
			case "MAG_SECONDARY": {
				if (count _weapons > 1) then {
					_magazine = getArray (configFile >> "CfgWeapons" >> (_weapons select 1) >> "magazines") select 0;

					_cancel = true;
					[_magazine] call _fnc_repeated_purchase;
				} else {
					_cancel = true;
				};
			};
		};
	} else {
		/* Normal weapon */
		_weapon = _args select 0;

		/* Remove old weapon of the same slot type   NOTE: if this proves unreliable, simply store what guns each player buys instead */
		_weapons = [] call fnc_player_getWeapons;

		if ([_weapon, 0, primary_weapons] call fnc_search_array >= 0) then {
			if !((_weapons select 0) == "") then {
				_oldweapon = (_weapons select 0);
				_oldmagazine = getArray (configFile >> "CfgWeapons" >> _oldweapon >> "magazines") select 0;

				_pref = [_weapons select 0, 0, prefMags] call fnc_search_array;
				if (_pref >= 0) then {
					_oldmagazine = (_prefMags select _pref) select 1;
				};

				player removeMagazines _oldmagazine;
				player removeWeapon _oldweapon;
			};
		} else {
			if ([_weapon, 0, secondary_weapons] call fnc_search_array >= 0) then {
				if (count _weapons > 1) then {
					_oldweapon = (_weapons select 1);
					_oldmagazine = getArray (configFile >> "CfgWeapons" >> _oldweapon >> "magazines") select 0;
					player removeMagazines _oldmagazine;
					player removeWeapon _oldweapon;
				};
			};
		};
	

		/* Give the player a free magazine with the gun */
		_magazine = getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines") select 0;
		_pref = [_weapon, 0, prefMags] call fnc_search_array;
		if (_pref >= 0) then {
			_magazine = (prefMags select _pref) select 1;
		};
		player addMagazine _magazine;

		/* Give him the gun */
		player addWeapon _weapon;

		/* Switch to the gun if its a primary or secondary if no primary is present */
		if ([_weapon, 0, primary_weapons] call fnc_search_array >= 0) then {
			player selectWeapon _weapon;
		} else {
			if ((([] call fnc_player_getWeapons) select 0 ) == "") then {
				player selectWeapon _weapon;
			};
		};
	};

	/* Modify players money */
	if !(_cancel) then {
		[-_price] call fnc_modify_money; 
	};
} else {
	hint "Insufficient funds";
};